﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserLoginDemo.Model;

namespace UserLoginDemo.DAl
{
    public class UserStudentServiceDAL
    {
        public UserStuentLogin GetUserStuentLogin(string userName)
        {
            string sql = "select * from UserStudent where UserName=@UserName";
            SqlParameter[] pars = {
                                  new SqlParameter("@UserName",SqlDbType.NVarChar,32)
                                  };
            pars[0].Value = userName;
            DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text, pars);//
            UserStuentLogin userInfo = null;
            if (da.Rows.Count > 0)
            {
                userInfo = new UserStuentLogin();
                LoadEntity(userInfo, da.Rows[0]);
            }
            return userInfo;
        }

        private void LoadEntity(UserStuentLogin userInfo, DataRow dataRow)
        {
            userInfo.UserName = dataRow["UserName"] != DBNull.Value ? dataRow["UserName"].ToString() : string.Empty;
            userInfo.Password = dataRow["Password"] != DBNull.Value ? dataRow["Password"].ToString() : string.Empty;
            userInfo.Id = Convert.ToInt32(dataRow["Id"]);
        }
    }
}
